package com.net.oil.util.share;

import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.util.DisplayUtil;
import com.net.oil.widget.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;

import androidx.fragment.app.DialogFragment;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;


public class ShareDialogFragment extends DialogFragment {

    @BindView(R.id.share_menu)
    GridView shareMenu;
    @BindView(R.id.cancel)
    Button cancel;
    Unbinder unbinder;
    @BindView(R.id.pop_view)
    View popView;

    /* 分享的链接地址 */
    private String mShareUrl = "";
    /* 分享的标题 */
    private String mShareTitle = "";

    public ArrayList<ShareMenuItem> platforms = new ArrayList<>();

    public static ShareDialogFragment newInstance(String mShareUrl, String mShareTitle) {
        //创建一个带有参数的Fragment实例
        ShareDialogFragment fragment = new ShareDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString(Constant.SHARE_URL, mShareUrl);
        bundle.putString(Constant.SHARE_TITLE, mShareTitle);
        fragment.setArguments(bundle);//把参数传递给该DialogFragment
        return fragment;
    }

    private void intiShareData() {
        Bundle mbundle = getArguments();
        if (mbundle != null) {
            mShareUrl = mbundle.getString(Constant.SHARE_URL);
            mShareTitle = mbundle.getString(Constant.SHARE_TITLE);
        }
    }
    @Override
    public void onStart() {
        super.onStart();
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        window.setAttributes(params);
        window.setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
        window.setBackgroundDrawableResource(android.R.color.transparent);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setStyle(STYLE_NO_FRAME, R.style.ThemeLightDialog);

        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        intiShareData();
        View view = inflater.inflate(R.layout.activity_share_menu, container, false);
        unbinder = ButterKnife.bind(this, view);
        initPlatforms();
        initView();
        setDialogPositionTop();
        return view;
    }

    private void initView() {
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        popView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        ShareAdapter shareAdapter = new ShareAdapter(getActivity(), platforms);
        shareMenu.setAdapter(shareAdapter);

        shareMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0){
                    share(Wechat.NAME);
                }else if (position == 1){
                    share(WechatMoments.NAME);
                }
            }
        });
    }

    private void setDialogPositionTop() {
        Window window = getDialog().getWindow();
        window.setGravity(Gravity.TOP | Gravity.RIGHT);
        WindowManager.LayoutParams params = window.getAttributes();
        params.y = DisplayUtil.dip2px(90);
        params.x = DisplayUtil.dip2px(0);
        window.setAttributes(params);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void initPlatforms() {

        platforms.clear();
        ShareMenuItem mShareMenuItem = new ShareMenuItem("微信", R.mipmap.umeng_socialize_wechat);
        platforms.add(mShareMenuItem);//微信

        mShareMenuItem = new ShareMenuItem("微信朋友圈", R.mipmap.umeng_socialize_wxcircle);
        platforms.add(mShareMenuItem);//微信朋友圈

    }

    private void share(String name){
        Platform platform = ShareSDK.getPlatform(name);
        Platform.ShareParams shareParams = new Platform.ShareParams();
        shareParams.setTitle(mShareTitle);
        shareParams.setUrl(mShareUrl);
        shareParams.setShareType(Platform.SHARE_WEBPAGE);
        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                ToastUtil.showSuccessToast(getContext(), "分享成功了");
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                ToastUtil.showErrorToast(getContext(), "分享失败了");
            }

            @Override
            public void onCancel(Platform platform, int i) {
                ToastUtil.showErrorToast(getContext(), "分享取消了");
            }
        });
        platform.share(shareParams);
    }
}
